﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;

namespace Thep
{
    public partial class FrmHeSoAnToan : Form
    {
        private string HeSoAnToanID = "";
        #region Database
        OleDbConnection conn;
        OleDbCommand cmd;

        public void Connect()
        {
            string connectstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database.a;Persist Security Info=False";
            conn = new OleDbConnection(connectstring);
            cmd = conn.CreateCommand();
        }
        #endregion

        public FrmHeSoAnToan()
        {
            InitializeComponent();
        }

        #region He so an toan methods

        public void CleanText() 
        {
            txtBeTong.Text = "";
            txtBulong.Text = "";
            txtCotThep.Text = "";
            txtDuongHan.Text = "";
            txtKetCauThep.Text = "";
            txtTenHeSo.Text = "";
        }

        public void ActiveButton(bool them, bool luu, bool xoa, bool huy)
        {
            btnThem.Enabled = them;
            btnLuu.Enabled = luu;
            btnXoa.Enabled = xoa;
            btnHuy.Enabled = huy;
        }

        public void ActiveText(bool flag)
        {
            txtTenHeSo.Enabled = flag;
            txtKetCauThep.Enabled = flag;
            txtDuongHan.Enabled = flag;
            txtCotThep.Enabled = flag;
            txtBulong.Enabled = flag;
            txtBeTong.Enabled = flag;
        }

        public DataTable LoadHeSoAnToan()
        {
            Connect();
            DataTable HeSoAnToan = new DataTable();
            HeSoAnToan.Columns.Add("HeSoAnToanID", typeof(int));
            HeSoAnToan.Columns.Add("TenHeSo", typeof(string));
            try
            {
                cmd.CommandText = "SELECT * FROM HeSoAnToan";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    DataRow dr = HeSoAnToan.NewRow();
                    dr["HeSoAnToanID"] = int.Parse(reader["HeSoAnToanID"].ToString());
                    dr["TenHeSo"] = reader["TenHeSo"].ToString();
                    HeSoAnToan.Rows.Add(dr);
                }
                return HeSoAnToan;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public void InsertHeSoAnToan(HeSoAnToan HeSoAnToan)
        {
            try
            {
                cmd.CommandText = "INSERT INTO HeSoAnToan(TenHeSo,KetCauThep,CotThep,BeTong,DuongHan,Bulong) VALUES('" + HeSoAnToan.TenHeSo + "','" + HeSoAnToan.KetCauThep + "','" + HeSoAnToan.CotThep + "','" + HeSoAnToan.BeTong + "','" + HeSoAnToan.DuongHan + "','" + HeSoAnToan.BuLong + "')";
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public HeSoAnToan FindHeSoAnToan(int t)
        {
            try
            {
                Connect();
                HeSoAnToan thep = new HeSoAnToan();
                cmd.CommandText = "SELECT * FROM HeSoAnToan WHERE HeSoAnToanID=" + t;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    thep.HeSoAnToanID = int.Parse(reader["HeSoAnToanID"].ToString());
                    thep.TenHeSo = reader["TenHeSo"].ToString();
                    thep.KetCauThep = float.Parse(reader["KetCauThep"].ToString());
                    thep.CotThep = float.Parse(reader["CotThep"].ToString());
                    thep.BeTong = float.Parse(reader["BeTong"].ToString());
                    thep.DuongHan = float.Parse(reader["DuongHan"].ToString());
                    thep.BuLong = float.Parse(reader["BuLong"].ToString());
                }


                return thep;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void DeleteHeSoAnToan(int id)
        {
            try
            {
                cmd.CommandText = "DELETE FROM HeSoAnToan WHERE HeSoAnToanID = " + id;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }
        }

        public void Delete(HeSoAnToan m)
        {

            try
            {
                cmd.CommandText = "DELETE FROM KetQua WHERE HeSoAnToanID = " + m.HeSoAnToanID;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        public KetQua FindKetQua(HeSoAnToan m)
        {
            try
            {
                Connect();
                KetQua ketqua = new KetQua();
                cmd.CommandText = "SELECT * FROM KetQua WHERE HeSoAnToanID = " + m.HeSoAnToanID;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ketqua.KetQuaID = int.Parse(reader["KetQuaID"].ToString());
                }
                return ketqua;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn != null) conn.Close();
            }

        }

        #endregion

        #region He So An Toan Event

        private void FrmHeSoAnToan_Load(object sender, EventArgs e)
        {
            Connect();
            ActiveText(false);
            ActiveButton(true, false, false, false);
            grcHeSoAnToan.DataSource = LoadHeSoAnToan();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText();
            grcHeSoAnToan.Enabled = false;
            ActiveButton(false, true, false, true);
            ActiveText(true);
            txtTenHeSo.Focus();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Connect();
            CleanText();
            ActiveButton(true, false, false, false);
            grcHeSoAnToan.Enabled = true;
            ActiveText(false);
            HeSoAnToanID = "";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Connect();
            if (HeSoAnToanID != "")
            {
                HeSoAnToan thep = FindHeSoAnToan(int.Parse(HeSoAnToanID));
                if (thep != null)
                {
                    KetQua k = FindKetQua(thep);
                    if (k.KetQuaID != 0)
                    {
                        DialogResult rs = MessageBox.Show("Thông số này đã được dùng để tính toán, nếu bạn xóa thông số này, chương trình sẽ xóa cả kết quả dùng thông số này để tính toán!! Bạn có chắc muốn xóa??", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (rs == DialogResult.Yes)
                        {
                            DeleteHeSoAnToan(thep.HeSoAnToanID);
                            Delete(thep);
                        }
                    }
                    else { 
                         DeleteHeSoAnToan(thep.HeSoAnToanID);
                            
                    }
                     CleanText();
                            grcHeSoAnToan.DataSource = LoadHeSoAnToan();
                            ActiveText(false);
                            ActiveButton(true, false, false, false);
                }
                else
                    MessageBox.Show("Hệ số ko tồn tại!");
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Connect();
            //Kiem tra o text rong
            if (txtTenHeSo.Text == "") { txtTenHeSo.ErrorText = "Nhập tên Hệ Số"; return; }

            //Khai bao 1 bien rong
            HeSoAnToan thep = new HeSoAnToan();


            //gan du lieu cho bien rong tu cac o text
            thep.TenHeSo = txtTenHeSo.Text.Trim();
            thep.KetCauThep =txtKetCauThep.Text != "" ? float.Parse(txtKetCauThep.Text.Trim()) : 0;
            thep.CotThep = txtCotThep.Text != "" ? float.Parse(txtCotThep.Text.Trim()) : 0;
            thep.BeTong = txtBeTong.Text != "" ? float.Parse(txtBeTong.Text.Trim()):0;
            thep.DuongHan = txtDuongHan.Text != "" ? float.Parse(txtDuongHan.Text.Trim()):0;
            thep.BuLong = txtBulong.Text != "" ? float.Parse(txtBulong.Text.Trim()):0;
            //Them vao database
            InsertHeSoAnToan(thep);

            CleanText();
            ActiveButton(true, false, false, false);
            grcHeSoAnToan.Enabled = true;
            ActiveText(false);
            grcHeSoAnToan.DataSource = LoadHeSoAnToan();
        }

        private void grvCot_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Connect();
            if (grvHeSoAnToan.SelectedRowsCount > 0)
            {

                HeSoAnToan thep = FindHeSoAnToan((int)grvHeSoAnToan.GetRowCellValue(grvHeSoAnToan.GetSelectedRows()[0], "HeSoAnToanID"));

                if (thep != null)
                {
                    HeSoAnToanID = thep.HeSoAnToanID.ToString();
                    txtTenHeSo.Text = thep.TenHeSo;
                    txtKetCauThep.Text = thep.KetCauThep.ToString();
                    txtCotThep.Text = thep.CotThep.ToString();
                    txtBeTong.Text = thep.BeTong.ToString();
                    txtDuongHan.Text = thep.DuongHan.ToString();
                    txtBulong.Text = thep.BuLong.ToString();
                    
                    ActiveButton(true, false, true, true);
                }
            }
        }

        #endregion

    }
}
